package com.yunyi.common.utils.file;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

/**
 * @ClassName: Base64FileUtil
 * @Author: ZouLi
 * @Description: Base64码转文件工具类
 * @Date: 2023/9/6 11:58
 **/
@Slf4j
public class Base64FileUtil implements MultipartFile {
    private final byte[] imgContent;
    private final String header;

    public Base64FileUtil(byte[] imgContent, String header) {
        this.imgContent = imgContent;
        this.header = header.split(";")[0];

    }

    @Override
    public String getName() {
        // TODO - implementation depends on your requirements
        return System.currentTimeMillis() + Math.random() + "." + header.split("/")[1];
    }

    @Override
    public String getOriginalFilename() {
        // TODO - implementation depends on your requirements
        return System.currentTimeMillis() + (int)Math.random() * 10000 + "." + header.split("/")[1];
    }

    @Override
    public String getContentType() {
        // TODO - implementation depends on your requirements
        return header.split(":")[1];
    }

    @Override
    public boolean isEmpty() {
        return imgContent == null || imgContent.length == 0;
    }

    @Override
    public long getSize() {
        return imgContent.length;
    }

    @Override
    public byte[] getBytes() {
        return imgContent;
    }

    @Override
    public InputStream getInputStream() {
        return new ByteArrayInputStream(imgContent);
    }

    @Override
    public void transferTo(File dest) throws IOException, IllegalStateException {
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(dest);
            out.write(imgContent);
            out.close();
        } catch (Exception e) {
            log.error("transferTos:{}",e);
        }finally {
            if(null!=out) {
                try{
                    out.flush();
                }catch(Exception e){

                }
                try{
                    out.close();
                }catch(Exception e){

                }
            }
        }

    }

    public static MultipartFile base64ToMultipart(String base64) {
        try {
            String[] baseStrs = base64.split(",");


            byte[] b;
            log.info("**baseStrs长度："+baseStrs.length+ "***base64ToMultipart信息："+baseStrs);
            b = Base64.getDecoder().decode(baseStrs[1]);
            log.info("byte[]--b信息："+b);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            return new Base64FileUtil(b, baseStrs[0]);
        } catch (Exception e) {
            log.error("transferTo:{}",e);
            return null;
        }
    }



    /**
     * 文件转base64字符串
     * @param filePath
     * @return
     */
    public static String encryptToBase64(String filePath) {
        if (filePath == null) {
            return null;
        }
        try {
            byte[] b = Files.readAllBytes(Paths.get(filePath));
            return org.apache.commons.codec.binary.Base64.encodeBase64String(b);
        } catch (IOException e) {
            log.error("encryptToBase64:{}",e);
        }
        return null;
    }

}
